package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.RolePers;
import cn.kgc.vue.mapper.RolePersMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.service.PermissionService;
import cn.kgc.vue.mapper.PermissionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_permission】的数据库操作Service实现
* @createDate 2024-04-15 11:33:52
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService{


    @Resource
    private RolePersMapper rolePersMapper;
    @Override
    public ResponseResult delte(Integer id) {

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid,id);

        List<Permission> list = baseMapper.selectList(lambda);

        if (ObjectUtils.isNotNull(list)){
            return  ResponseResult.fail().message("权限包含子节点不能删除");
        }

        // 删除权限
        baseMapper.deleteById(id);


        // 删除权限关联数据  role_permission
        LambdaQueryWrapper<RolePers> lambda1 = new QueryWrapper<RolePers>().lambda();
        lambda1.eq(RolePers::getPerId,id);
        rolePersMapper.delete(lambda1);


        return ResponseResult.success().message("删除成功");
    }
}




